﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace Wee
{
    public static class ParameterList
    {
        #region 添加参数
        /// <summary>
        /// 添加SQL参数
        /// </summary>
        /// <param name="ParameterName">参数名</param>
        /// <param name="DbType">参数类型 SqlDbType</param>
        /// <param name="Value">参数值</param>
        /// <param name="Size">参数尺寸</param>
        /// <param name="Direction">描述</param>
        /// <param name="NotNull">不允许为空</param>
        /// <returns>SqlParameter</returns>
        public static SqlParameter Add(string ParameterName, SqlDbType DbType, object Value, int Size, ParameterDirection Direction, bool NotNull = false)
        {
            SqlParameter parameter = new SqlParameter(ParameterName, DbType);
            parameter.Value = DBConvert(Value, DbType, NotNull);
            if (Size > 0) parameter.Size = Size;
            parameter.Direction = Direction;
            return parameter;
        }

        /// <summary>
        /// 添加SQL参数
        /// </summary>
        /// <param name="ParameterName">参数名</param>
        /// <param name="DbType">参数类型 SqlDbType</param>
        /// <param name="Value">参数值</param>
        /// <param name="Size">参数尺寸</param>
        /// <param name="NotNull">不允许为空</param>
        /// <returns></returns>
        public static SqlParameter Add(string ParameterName, SqlDbType DbType, object Value, int Size, bool NotNull = false)
        {
            return Add(ParameterName, DbType, Value, Size, ParameterDirection.Input, NotNull);
        }

        /// <summary>
        /// 添加SQL参数
        /// </summary>
        /// <param name="ParameterName">参数名</param>
        /// <param name="DbType">参数类型 SqlDbType</param>
        /// <param name="Value">参数值</param>
        /// <param name="NotNull">不允许为空</param>
        /// <returns></returns>
        public static SqlParameter Add(string ParameterName, SqlDbType DbType, object Value, bool NotNull = false)
        {
            return Add(ParameterName, DbType, Value, 0, ParameterDirection.Input, NotNull);
        }

        /// <summary>
        /// 添加SQL参数
        /// </summary>
        /// <param name="ParameterName">参数名</param>
        /// <param name="DbType">参数类型 SqlDbType</param>
        /// <param name="Direction">描述</param>
        /// <returns></returns>
        public static SqlParameter Add(string ParameterName, SqlDbType DbType, ParameterDirection Direction)
        {
            SqlParameter parameter = new SqlParameter(ParameterName, DbType);
            parameter.Direction = Direction;
            return parameter;
        }
        #endregion

        /// <summary>
        /// SQL数据类型转换
        /// </summary>
        /// <param name="Value">被转换的值</param>
        /// <param name="DbType">目标类型</param>
        /// <param name="NotNull">不允许为空</param>
        /// <returns>object类型</returns>
        public static object DBConvert(object Value, SqlDbType DbType, bool NotNull, int size = 0)
        {
            object returnValue = null;
            try
            {
                switch (DbType)
                {
                    case SqlDbType.DateTime: { returnValue = Convert.ToDateTime(Value); break; }
                    case SqlDbType.Int: { returnValue = Convert.ToInt32(Value); break; }
                    case SqlDbType.Float: { returnValue = Convert.ToSingle(Value); break; }
                    case SqlDbType.Decimal: { returnValue = Convert.ToDecimal(Value); break; }
                    case SqlDbType.Money: { returnValue = Convert.ToDouble(Value); break; }
                    case SqlDbType.Bit: { returnValue = Convert.ToBoolean(Value); break; }
                    default:
                        {
                            string val = Value.ToString();
                            if (size > 0)
                            {
                                if (size > val.Length)
                                {
                                    size = val.Length;
                                }

                                returnValue = val.Substring(0, size);
                            }
                            returnValue = val;
                            break;
                        }
                }
            }
            catch { if (NotNull) throw new Exception("该字段不能为空"); }
            return returnValue;
        }
    }
}
